package com.atguigu.spring.config;

import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.ConfigurableBootstrapContext;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

import java.time.Duration;

public class MyApplicationListener implements SpringApplicationRunListener {

    public void starting(ConfigurableBootstrapContext bootstrapContext) {
        System.out.println("☆☆☆☆☆☆☆☆☆☆starting☆☆☆☆☆☆☆☆☆☆");
    }

    /**
     * Called once the environment has been prepared, but before the
     * {@link ApplicationContext} has been created.
     * @param bootstrapContext the bootstrap context
     * @param environment the environment
     */
    public void environmentPrepared(ConfigurableBootstrapContext bootstrapContext,
                                     ConfigurableEnvironment environment) {
        System.out.println("☆☆☆☆☆☆☆☆☆☆environmentPrepared☆☆☆☆☆☆☆☆☆☆");
    }

    /**
     * Called once the {@link ApplicationContext} has been created and prepared, but
     * before sources have been loaded.
     * @param context the application context
     */
    public void contextPrepared(ConfigurableApplicationContext context) {
        System.out.println("☆☆☆☆☆☆☆☆☆☆contextPrepared☆☆☆☆☆☆☆☆☆☆");
    }

    /**
     * Called once the application context has been loaded but before it has been
     * refreshed.
     * @param context the application context
     */
    public void contextLoaded(ConfigurableApplicationContext context) {
        System.out.println("☆☆☆☆☆☆☆☆☆☆contextLoaded☆☆☆☆☆☆☆☆☆☆");
    }

    /**
     * The context has been refreshed and the application has started but
     * {@link CommandLineRunner CommandLineRunners} and {@link ApplicationRunner
     * ApplicationRunners} have not been called.
     * @param context the application context.
     * @param timeTaken the time taken to start the application or {@code null} if unknown
     * @since 2.6.0
     */
    public void started(ConfigurableApplicationContext context, Duration timeTaken) {
        System.out.println("☆☆☆☆☆☆☆☆☆☆started☆☆☆☆☆☆☆☆☆☆");
    }

    /**
     * Called immediately before the run method finishes, when the application context has
     * been refreshed and all {@link CommandLineRunner CommandLineRunners} and
     * {@link ApplicationRunner ApplicationRunners} have been called.
     * @param context the application context.
     * @param timeTaken the time taken for the application to be ready or {@code null} if
     * unknown
     * @since 2.6.0
     */
    public void ready(ConfigurableApplicationContext context, Duration timeTaken) {
        System.out.println("☆☆☆☆☆☆☆☆☆☆ready☆☆☆☆☆☆☆☆☆☆");
    }

    /**
     * Called when a failure occurs when running the application.
     * @param context the application context or {@code null} if a failure occurred before
     * the context was created
     * @param exception the failure
     * @since 2.0.0
     */
    public void failed(ConfigurableApplicationContext context, Throwable exception) {
        System.out.println("☆☆☆☆☆☆☆☆☆☆failed☆☆☆☆☆☆☆☆☆☆");
    }

}
